Global class CXGCampaignStopPauseUnpause
{   
	WebService static void CampaignStop(String campaignId)
	{         
		// List for holding records that this code will ultimately update 
		List<CXG_Campaign_Accounts__c> cmclosedlist = new List<CXG_Campaign_Accounts__c>();       
		Campaign cam = [select Startdate, EndDate, Name from Campaign Where Id = :campaignId];
		Id cxgCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member');       
		Id usr = UserInfo.getUserId();
		for (CXG_Campaign_Accounts__c cm : [select Id from CXG_Campaign_Accounts__c 
											where Campaign__c = :campaignId 
											and Status__c in ('New', 'In Progress - Not Contacted', 'In Progress - Contacted', 'Completed') 
											and RecordTypeId = :cxgCampaignMemberRecordTypeId])
		{
			cm.OwnerId = usr; 
			cm.Status__c = 'Closed'; 
			cm.IscampaignActive__c = false;
			cmclosedlist.add(cm); 
		}
		update cmclosedlist;
		
		cam.EndDate = Date.Today();    
		cam.IsActive = false;
		update cam;         
	}
         
	WebService static void CampaignPause(String campaignId)
	{
		Campaign cam = [select Id, RecordTypeId, Paused__c from Campaign where Id = :campaignId];
		Id cxgCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member');       
		Id cxgPausedCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member_Locked');       
		Id cxgPausedCampaignRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('Campaign', 'CXG_Paused_Campaign');       
		 
		// List for holding records that this code will ultimately update 
		List<CXG_Campaign_Accounts__c> cmpausedlst = new List<CXG_Campaign_Accounts__c>();
		for (CXG_Campaign_Accounts__c cm : [select Id, RecordTypeId from CXG_Campaign_Accounts__c 
										where Campaign__c = :campaignId 
										and Status__c in ('New', 'In Progress - Not Contacted') 
										and Control_Group_Member__c = false 
										and RecordTypeId = :cxgCampaignMemberRecordTypeId])
		{
			cm.RecordTypeId = cxgPausedCampaignMemberRecordTypeId; 
			cm.Status__c = 'On Hold';
			cm.Campaign_Paused__c = true;
			cmpausedlst.add(cm);                  
		}		
		update cmpausedlst; 
		
		cam.RecordTypeId = cxgPausedCampaignRecordTypeId;
		cam.Paused__c = true;
		update cam;
	}
         
	WebService static void CampaignUnPause(String campaignId)
	{
         Campaign cam = [select Id,RecordTypeId,Paused__c from Campaign where Id = :campaignId];
         Id cxgCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member');       
         Id cxgCampaignRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('Campaign', 'CXG_Campaign');       
         Id cxgPausedCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member_Locked');       
        
         // List for holding records that this code will ultimately update          
         List<CXG_Campaign_Accounts__c> cmunpausedlst = new List<CXG_Campaign_Accounts__c>();
         for(CXG_Campaign_Accounts__c cm : [select Id, RecordTypeId from CXG_Campaign_Accounts__c 
         								where Campaign__c = :campaignId 
         								and (Status__c = 'On Hold' or Campaign_Paused__c = true) 
         								and Control_Group_Member__c = false 
         								and RecordTypeId=:cxgPausedCampaignMemberRecordTypeId])
         {
         	cm.RecordTypeId = cxgCampaignMemberRecordTypeId;
         	cm.Status__c = 'New';
         	cm.Campaign_Paused__c = false;
         	cmunpausedlst.add(cm);
         }
         update cmunpausedlst;
         
         cam.RecordTypeId = cxgCampaignRecordTypeId;
         cam.Paused__c = false;
         update cam;
	}
         
	static testMethod void testCmapignStoppauseunpause()  
	{
		Id cxgCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member');       
		Id cxgPausedCampaignMemberRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member_Locked');       
		Id cxgPausedCampaignRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('Campaign', 'CXG_Paused_Campaign');       
		Id cxgCampaignRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('Campaign', 'CXG_Campaign');       
		
		Campaign c = new Campaign();
		c.Name = 'TestCampaign';   
		c.StartDate = Date.Today();    
		c.IsActive = true;   
		c.RecordTypeId = cxgCampaignRecordTypeId; 
		c.Number_of_Control_Group_Members__c = 20;    
		insert c;
		
		Account acct = new Account();    
		acct.Name = 'TestStart';    
		insert acct;   
		
		CXG_Campaign_Accounts__c cm = new CXG_Campaign_Accounts__c(); 
		cm.Campaign__c = c.Id;  
		cm.RecordTypeId = cxgCampaignMemberRecordTypeId;  
		cm.Customer__c = acct.id;     
		cm.Status__c = 'New';     
		insert cm;
		
		c.Paused__c = true;
		update c;
		
		cm.Status__c = 'On Hold';
		cm.RecordTypeId = cxgPausedCampaignMemberRecordTypeId;
		cm.Campaign_Paused__c = true;
		update cm;
		
		c.Paused__c = false;
		c.IsActive = false;
		update c;  
		
		cm.Status__c = 'Closed';
		update cm;
		
		CXGCampaignStopPauseUnpause.CampaignStop(c.Id);
		CXGCampaignStopPauseUnpause.CampaignPause(c.Id);
		CXGCampaignStopPauseUnpause.CampaignUnPause(c.Id);
	}
}